//
//  WorkViewController.m
//  Testhuitu
//
//  Created by gg on 7/1/13.
//  Copyright (c) 2013 sunland.com. All rights reserved.
//

#import "WorkViewController.h"
#import "PlanLine.h"
#import <QuartzCore/QuartzCore.h>



@interface WorkViewController ()

@end

@implementation WorkViewController
@synthesize imageView;
@synthesize scrollView;
@synthesize monthButton;
@synthesize massageView;
@synthesize a;
@synthesize array;
@synthesize massageImageView;
@synthesize photoImageViewArray;
@synthesize massageViewArray;
@synthesize label;
@synthesize buttonArray;
@synthesize monthlabel;
@synthesize monthLabelArray;
@synthesize y;
@synthesize animationImageView;
@synthesize AddimageView;
@synthesize textView;
@synthesize animation1;
@synthesize arrayMonths;
@synthesize b;
@synthesize stealthButton;
@synthesize c;
@synthesize mArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
//        self.title = @"Time line";
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    [self.navigationController setToolbarHidden:NO];
//    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//    self.navigationController.toolbar.tintColor = [UIColor blackColor];
	// Do any additional setup after loading the view.
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];

    NSDictionary *param3= @{@"userId": ID};
    
    [UserDAL getBabyHistoryParams:param3 withBlock:^(NSArray *data, NSError *error) {
        
        ////NSLog(@"都是我的%@",data);
        
        for (NSDictionary * dic in data) {
            
            NSArray *data = dic[@"images"];

            NSString * timeStamp = [dic objectForKey:@"timeStamp"];
            
            NSString * title = [dic objectForKey:@"title"];

            ////NSLog(@"%@,%@,%@",data,timeStamp,title);
            
            
            
            
            
            
            
            
            
        }
        
        
        
        
        
        
        
    }];
    
    
    
    
    a = 0;
    y = 0;
    photoImageViewArray = [[NSMutableArray alloc]initWithCapacity:10];
    self.array = [[NSMutableArray alloc]initWithCapacity:10];
    NSArray *array1 = [NSArray arrayWithObjects:@"1.jpg",@"2.jpg", nil];
    

    for (int i = 0; i < array1.count; i++)
    {
        UIImage *image = [UIImage imageNamed:[array1 objectAtIndex:i]];
        [array addObject:image];
    }
    
    
    NSArray *array4 = [NSArray arrayWithObjects:@"1月",@"2月",@"3月",@"4月",/*@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月",*/ nil];
    
    
 
    arrayMonths = [array4 copy];
    
    int number = [arrayMonths count];
    
   
    
    UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavbarHight)];
    UIImage *backgroundImage = [UIImage imageNamed:@""];
    [backgroundImageView setImage:backgroundImage];
    [self.view addSubview:backgroundImageView];
    
    [self makeScrollView:number];
    
    //调整显得坐标
    self.imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.contentSize.height);
    [scrollView addSubview:imageView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    PlanLine *plan = [[PlanLine alloc]init];
    
    plan.delegate = self;
    
    [plan setImageView:imageView setlineWidth:5.0 setColorRed:0 ColorBlue:1 ColorGreen:0 Alp:1 setBeginPointX:60 BeginPointY:0 setOverPointX:60 OverPointY:scrollView.contentSize.height];
    
    
//    UIImageView *imageViewline = [[UIImageView alloc]initWithFrame:CGRectMake(60, 0, 4, scrollView.contentSize.height)];
//    UIImage *imageline = [UIImage imageNamed:@"line2.png"];
//    [imageViewline setImage:imageline];
//    [scrollView addSubview:imageViewline];

    [self makeMonthButton:number];
    [self makeView:number];
    
    /*
     原始方法
    */
//    //需要实例化imageView并添加视图
//    UIImageView *imageView2 = [[[UIImageView alloc]initWithFrame:self.view.frame]autorelease];
//    [self.view addSubview:imageView2];
//    //调用方法
//    [self setImageView:imageView2 setlineWidth:5.0 setColorRed:20 ColorBlue:20 ColorGreen:20 Alp:1 setBeginPointX:100 BeginPointY:100 setOverPointX:100 OverPointY:400];
 
    //设置导航栏头文字
    self.navigationItem.title = @"家园统合";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:UIColorWithRGBA(239, 88, 88, 1)];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
//    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    //自定义左右按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(turntolast)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;
    

    
    
    
}
-(void)turntolast{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)makeScrollView:(int)number
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavbarHight, SCREEN_WIDTH, SCREEN_HEIGHT-NavbarHight)];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, number*50+200);
    [self.view addSubview:scrollView];
}

-(void)makeMonthButton:(int)number
{
    self.monthLabelArray = [[NSMutableArray alloc]initWithCapacity:10];
    self.buttonArray = [[NSMutableArray alloc]initWithCapacity:10];
    for (int i = 0; i < number; i++)
    {
        self.monthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:@"button.png"];
        [monthButton setImage:image forState:UIControlStateNormal];
        monthButton.frame = CGRectMake(46, i*50, 30, 30);
        monthButton.tag = i;
        [monthButton addTarget:self action:@selector(openMassage:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:monthButton];
        [buttonArray addObject:monthButton];
        
        self.monthlabel = [[UILabel alloc]initWithFrame:CGRectMake(7, i*50, 40, 30)];
        monthlabel.text = [arrayMonths objectAtIndex:i];
        monthlabel.font = [UIFont fontWithName:@"Noteworthy-Light" size:10];
        monthlabel.textAlignment = 0;
        monthlabel.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:monthlabel];
        [monthLabelArray addObject:monthlabel];
        
    }
    
}

-(void)makeView:(int)number
{
    self.massageViewArray = [[NSMutableArray alloc]initWithCapacity:10];
    
    for (int i = 0; i < number; i++)
    {
        self.massageView = [[UIView alloc]initWithFrame:CGRectMake(80, i*50, 200, 30)];
        
        massageView.backgroundColor = [UIColor clearColor];
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, massageView.bounds.size.width, 30)];
        label.textAlignment= 1;
        int x = 50;
        NSString *str = [NSString stringWithFormat:@"点击展开详情%d",x];
        [label setText:str];
        label.font = [UIFont fontWithName:@"Noteworthy-Light" size:10];
        label.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:massageView];
        [massageView addSubview:label];
        [massageViewArray addObject:massageView];
        [massageView setUserInteractionEnabled:NO];
    }
}


-(void)openMassage:(id)sender
{

    
    
    UIButton *button = (UIButton *)sender;
    ////NSLog(@"tag = %d",button.tag);
    c = button.tag;
    ////NSLog(@"y = %d",y);
    ////NSLog(@"%d",photoImageViewArray.count);
    
    //移除功能
    if (b!=0)
    {
        [photoImageViewArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [photoImageViewArray removeAllObjects];
        [animationImageView removeFromSuperview];
        [AddimageView removeFromSuperview];
        [textView removeFromSuperview];
    }
    b++;
    
    //时间段线段
    y = button.tag;
    
    PlanLine *plan = [[PlanLine alloc]init];
    self.AddimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.contentSize.height)];
    [plan setImageView:AddimageView setlineWidth:5.0 setColorRed:255 ColorBlue:0 ColorGreen:0 Alp:1 setBeginPointX:60 BeginPointY:button.tag*50+30 setOverPointX:60 OverPointY:(button.tag + 1 )*50+200];
    animation1 = [CATransition animation];
    animation1.delegate =self;
    animation1.duration = 2.0f;
    animation1.timingFunction = UIViewAnimationCurveEaseInOut;
    animation1.type = kCATransitionMoveIn;
    animation1.subtype = kCATransitionReveal;

    [AddimageView.layer addAnimation:animation1 forKey:@"animation"];
    [AddimageView setUserInteractionEnabled:NO];
    
    
    
    
    
    [scrollView addSubview:AddimageView];
    
    //实例化弹出界面
    
    //整体框架
    [[massageViewArray objectAtIndex:button.tag] setFrame:CGRectMake(80, button.tag*50, [UIScreen mainScreen].bounds.size.width-90, 230)];
    
    CATransition *animation = [CATransition animation];
    animation.delegate =self;
    animation.duration = 2.0f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionMoveIn;
    animation.subtype = kCATransition;
    [[[massageViewArray objectAtIndex:button.tag] layer] addAnimation:animation forKey:@"animation"];
    [[massageViewArray objectAtIndex:button.tag] setUserInteractionEnabled:YES];
    UIView *view = [massageViewArray objectAtIndex:button.tag];
    [view.layer setShadowColor:[UIColor blackColor].CGColor];
    [view.layer setShadowOffset:CGSizeMake(10, 10)];
    [view.layer setShadowOpacity:0.5];
    view.backgroundColor = [UIColor purpleColor];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gotoNext)];
    
    [view addGestureRecognizer:singleTap];
    
//    [animation release];
    
    
    [[buttonArray objectAtIndex:button.tag] setFrame:CGRectMake(46, button.tag*50, 30, 30)];
    
    [[monthLabelArray objectAtIndex:button.tag] setFrame:CGRectMake(5, button.tag*50, 40, 30)];
    
    //图片实例化显示
    for (int i = 0; i < array.count; i++)
    {
        
        //四个照片
        self.massageImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0+i*([UIScreen mainScreen].bounds.size.width-90)/2, 230-90, ([UIScreen mainScreen].bounds.size.width-90)/2, 90)];
        [massageImageView setImage:[array objectAtIndex:i]];
        [[massageViewArray objectAtIndex:button.tag] addSubview:massageImageView];
        [photoImageViewArray addObject:massageImageView];
        
    }
    //动画视图
    //空白图
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 230-90-70, ([UIScreen mainScreen].bounds.size.width)-100-90, 70)];
    textView.text = @"详细请点击进入....";
    textView.textAlignment = 1;
//    textView.backgroundColor = [UIColor clearColor];
    textView.userInteractionEnabled = NO;
    [[massageViewArray objectAtIndex:button.tag] addSubview:textView];
    
    //动画窗口
    //滚动视图
    self.animationImageView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-100-90, 230-90-70, 100, 70)];
    [animationImageView setAnimationImages:array];
    [animationImageView setAnimationDuration:10];
//    [animationImageView setAlpha:0.5];
    [animationImageView setAnimationRepeatCount:0];
    [animationImageView startAnimating];
    
    [[massageViewArray objectAtIndex:button.tag] addSubview:animationImageView];
    
    
    //位置调整 分上下 注意！！！！
    //点击处上面的
    if (button.tag == 0)
    {
        ////NSLog(@"asdasd");
    }
    else
    {
        for (int i = 0; i < button.tag; i++)
        {
            [[massageViewArray objectAtIndex:i] setFrame:CGRectMake(80, 0+i*50, 200, 30)];
            [[massageViewArray objectAtIndex:i] setBackgroundColor:[UIColor clearColor]];
            [[massageViewArray objectAtIndex:i] setUserInteractionEnabled:NO];
            
            
            [[buttonArray objectAtIndex:i] setFrame:CGRectMake(46, 0+i*50, 30, 30)];
            
            [[monthLabelArray objectAtIndex:i] setFrame:CGRectMake(5, 0+i*50, 40, 30)];
            
        }
        
        [[massageViewArray objectAtIndex:button.tag-1] setFrame:CGRectMake(80, (button.tag-1)*50, 200, 30)];
        [[buttonArray objectAtIndex:button.tag-1] setFrame:CGRectMake(46, (button.tag-1)*50, 30, 30)];
        [[monthLabelArray objectAtIndex:button.tag-1] setFrame:CGRectMake(5, (button.tag-1)*50, 40, 30)];
        
    }
    //点击处下面的
    for (int i = button.tag + 1; i < [massageViewArray count]; i++)
    {
        [[massageViewArray objectAtIndex:i] setFrame:CGRectMake(80, 200+i*50, 200, 30)];
        [[massageViewArray objectAtIndex:i] setBackgroundColor:[UIColor clearColor]];
        [[massageViewArray objectAtIndex:i] setUserInteractionEnabled:NO];
        UIView *view = [massageViewArray objectAtIndex:i];
        CABasicAnimation *positionAnim=[CABasicAnimation animationWithKeyPath:@"position"];
        [positionAnim setFromValue:[NSValue valueWithCGPoint:CGPointMake(view.center.x, view.center.y-200)]];
        [positionAnim setToValue:[NSValue valueWithCGPoint:CGPointMake(view.center.x, view.center.y)]];
        [positionAnim setDelegate:self];
        [positionAnim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [positionAnim setDuration:0.5f];
        [view.layer addAnimation:positionAnim forKey:@"positon"];
        [view setCenter:CGPointMake(view.center.x, view.center.y)];
        
        
        
        [[buttonArray objectAtIndex:i] setFrame:CGRectMake(46, 200+i*50, 30, 30)];
        
        UIButton *button = [buttonArray objectAtIndex:i];
        CABasicAnimation *positionAnim1=[CABasicAnimation animationWithKeyPath:@"position"];
        [positionAnim1 setFromValue:[NSValue valueWithCGPoint:CGPointMake(button.center.x, button.center.y-200)]];
        [positionAnim1 setToValue:[NSValue valueWithCGPoint:CGPointMake(button.center.x, button.center.y)]];
        [positionAnim1 setDelegate:self];
        [positionAnim1 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [positionAnim1 setDuration:0.5f];
        [button.layer addAnimation:positionAnim1 forKey:nil];
        [button setCenter:CGPointMake(button.center.x, button.center.y)];
        
        [[monthLabelArray objectAtIndex:i] setFrame:CGRectMake(5, 200+i*50, 40, 30)];
        
        UILabel *label1 = [monthLabelArray objectAtIndex:i];
        CABasicAnimation *positionAnim2=[CABasicAnimation animationWithKeyPath:@"position"];
        [positionAnim2 setFromValue:[NSValue valueWithCGPoint:CGPointMake(label1.center.x, label1.center.y-200)]];
        [positionAnim2 setToValue:[NSValue valueWithCGPoint:CGPointMake(label1.center.x, label1.center.y)]];
        [positionAnim2 setDelegate:self];
        [positionAnim2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [positionAnim2 setDuration:0.5f];
        [label1.layer addAnimation:positionAnim2 forKey:@"positon"];
        [label1 setCenter:CGPointMake(label1.center.x, label1.center.y)];
        
    }
    
}


-(void)gotoNext
{

    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

@end
